﻿using GraphicROI;
using HalconDotNet;
using PCBase.UIControls;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace vciplib0
{

    public class PRM_BumpRegion
    {
        [Category("检测区域"), DisplayName("检测区域"), Description("检测区域，并集，差集"), Browsable(false)]
        public List<PRM_ROI> ROIs { get; set; } = new List<PRM_ROI>();

        public HRegion BumpRegion = null;

        public HRegion GenBumpRegion(HImage BaseImg , int BPthreshold = 35)
        {
            Bump_threshold = BPthreshold;

            double row, col;
            HRegion tmp = new HRegion();
            tmp.GenEmptyRegion();

            HRegion tmpAdd, tmpDiff;

            tmpAdd = new HRegion();
            tmpDiff = new HRegion();  // rojor region 实例化之后 调用 GenEmptyRegion()
            tmpAdd.GenEmptyRegion();
            tmpDiff.GenEmptyRegion();
            foreach (var item in ROIs)
            {
                ROI roi = item.GetViewROI();
                roi.setOperatorFlag(item.ROIMode); //设置ROI的模式
                switch (roi.getOperatorFlag())
                {
                    case ROI.POSITIVE_FLAG:
                        tmp = roi.getRegion();
                        tmpAdd = tmp.Union2(tmpAdd);
                        break;
                    case ROI.NEGATIVE_FLAG:
                        tmp = roi.getRegion();
                        tmpDiff = tmp.Union2(tmpDiff);
                        break;
                    default:
                        break;
                }
            }

            if (tmpAdd.AreaCenter(out row, out col) > 0)
            {
                tmp = tmpAdd.Difference(tmpDiff);
            }

            HImage tempImg = BaseImg.ReduceDomain(tmp);
            tmp = tempImg.Threshold(0.0, BPthreshold).Connection().FillUp().OpeningRectangle1(3, 3).ClosingRectangle1(5, 5)
                                    .ShapeTrans("convex").Union1();
            BumpRegion = tmp;
             
            return tmp;
        }

        public void ShowROI(HWndCtrl viewCtrler , ROIController roiCtrler , HWindowControl view)
        {
            try
            {
                roiCtrler.reset();
                int iSeq = 0;
                foreach (var item in ROIs)
                {
                    iSeq++;
                    ROI roi = item.GetViewROI();
                    roi.setOperatorFlag(item.ROIMode); //设置ROI的模式
                    roiCtrler.ROIList.Add(roi);    //设置ROI模式后，传给 ROIList
                    string strFlag = item.ROIMode == 21 ? "+ Region" : "- Region";
                    //mUCWafer.ROIItemAddToListView(iSeq.ToString(), "检测", "", strFlag, item.ROI);
                }
                viewCtrler.repaint();
                viewCtrler.setDispLevel(1);
                roiCtrler.paintData(view.HalconWindow);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return;
            }
        }

        public void ShowBumpRegion(HWndCtrl viewCtrler , ROIController roiCtrler )
        {

        }


        public override string ToString()
        {
            return string.Format("{0}-{1}- RoiNo:{2}", DetectClassificationName, DetectRemark, ROIs.Count);
        }

        [Category("1.检测项目"), DisplayName("a.检测类名"), Description("类型Defect的类型名称定义"), Browsable(true)]
        [EditorAttribute(typeof(CommonInputEditor), typeof(System.Drawing.Design.UITypeEditor)), TypeConverter(typeof(DefectTypeDefine))]
        public string DetectClassificationName { get; set; } = "2D AOI";  // "Chipping"

        public string DetectRemark {get; set;} = "";

        [CategoryAttribute("4.检测参数 Bump"), DisplayName("d.Bump区灰阶阈值定义"), Description("Bump区灰阶阈值 设置范围[15 - 55] ,默认值 35 灰阶低于参数，则归为Bump区"), Browsable(false)]
        public Double Bump_threshold { get; set; } = 35;

    }

}
